﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.Data;
using MLM.Base;

namespace MLM.AdminCP.Pages
{
    public partial class CreateBill : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string GetListProduct(string _pageIndex, string _pageSize)
        {
            ResponseData responseData = new ResponseData();
            int pageIndex = int.Parse(_pageIndex);
            int pageSize = int.Parse(_pageSize);
            MLMDataContext ct = new MLMDataContext();
            List<ProductInfo> products = new List<ProductInfo>();
            var userid = Permission.GetCurrentUserId();
            var role = Permission.GetCurrentRole();
            if (role != 1)
            {
                var myproducts = ct.UserStocks.Where(it => it.StockAvaiable > 0 && it.UserID == userid).OrderBy(it => it.ProductBaseID).Skip(pageSize * pageIndex - pageSize).Take(pageSize).Select(it => it.ProductBas);
                foreach (var productBase in myproducts)
                {
                    ProductInfo p = new ProductInfo();
                    p.Name = productBase.Name;
                    p.Thumbnail = productBase.ThumbImage;
                    p.Price = productBase.Price;
                    p.Point = productBase.Points;
                    p.ProductId = productBase.ProductBaseID;
                    p.ProductNumber = productBase.ProductNumber ?? "---------";
                    products.Add(p);
                }
                JavaScriptSerializer ser = new JavaScriptSerializer();
                responseData.Products = products;
                responseData.TotalRow = ct.ProductBases.Count();
                return ser.Serialize(responseData);
            }
            else
            {
                var productBases = ct.ProductBases.OrderBy(it => it.ProductBaseID).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
                foreach (var productBase in productBases)
                {
                    ProductInfo p = new ProductInfo();
                    p.Name = productBase.Name;
                    p.Thumbnail = productBase.ThumbImage;
                    p.Price = productBase.Price;
                    p.Point = productBase.Points;
                    p.ProductId = productBase.ProductBaseID;
                    p.ProductNumber = productBase.ProductNumber ?? "---------";
                    products.Add(p);
                }
                JavaScriptSerializer ser = new JavaScriptSerializer();
                responseData.Products = products;
                responseData.TotalRow = ct.ProductBases.Count();
                return ser.Serialize(responseData);
            }
        }
        public class ProductInfo
        {
            public string Name { get; set; }
            public string Thumbnail { get; set; }
            public long ProductId { get; set; }
            public string ProductNumber { get; set; }
            public long? Point { get; set; }
            public double? Price { get; set; }
        }

        public class ResponseData
        {
            public int TotalRow { get; set; }
            public List<ProductInfo> Products { get; set; }
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string CreateNewBill(string _userId, string _productIds, string _quantities, string _depoId)
        {
            long userId = long.Parse(_userId);
            long depoId = long.Parse(_depoId);
            MLMDataContext ct = new MLMDataContext();
            Invoice bill = new Invoice();
            bill.DateTimeCreated = DateTime.Now;
            bill.DepoID = depoId;
            bill.Status = -1;
            bill.UserID = userId;
            bill.IsManual = true;
            var buyer = ct.Users.FirstOrDefault(it => it.UserId == userId);
            long? countryId = 0;
            try
            {
                countryId = buyer.CountryID;
            }
            catch (Exception)
            {
                countryId = 0;
            }

            double? vat = 0;
            try
            {
                vat = ct.GroupTaxes.FirstOrDefault(it => it.CountryID == countryId && it.RoleID == buyer.RoleID).VAT;
            }
            catch (Exception)
            {
                vat = 0;
            }
            bill.CreatedBy = Permission.GetCurrentUserId();
            ct.Invoices.AddObject(bill);
            ct.SaveChanges();

            int i = 0;
            foreach (var productId in _productIds.Split(';'))
            {
                if (productId == "")
                {

                }
                else
                {
                    long pId = long.Parse(productId);
                    var productBase = ct.ProductBases.FirstOrDefault(it => it.ProductBaseID == pId);
                    var quantities = long.Parse(_quantities.Split(';')[i]);
                    ct.InvoiceDetails.AddObject(new InvoiceDetail
                                                 {
                                                     InvoiceID = bill.InvoiceID,
                                                     Point = productBase.Points,
                                                     Price = productBase.Price,
                                                     ProductBaseID = productBase.ProductBaseID,
                                                     Quantity = quantities,
                                                     VAT = vat
                                                 });
                }
                i++;
            }
            ct.SaveChanges();
            return bill.InvoiceID.ToString();
        }
    }
}